package cn.kpi.service.sys.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.kpi.dao.sys.RightsDao;
import cn.kpi.entity.sys.RoleEntity;
import cn.kpi.entity.sys.RoleMenuEntity;
import cn.kpi.entity.sys.RoleOrgEntity;
import cn.kpi.entity.sys.RoleUserEntity;
import cn.kpi.service.sys.IRightsService;

@Service
public class RightsServiceImpl implements IRightsService{
	@Autowired
	private RightsDao rightsDao;

	public List<RoleEntity> GetRoleList(String where)throws Exception{
		try{
			return rightsDao.GetRoleList(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	public List<RoleUserEntity> GetRoleUserList(String where,Long roleid)throws Exception{
		try{
			return rightsDao.GetRoleUserList(where,roleid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	@Transactional
	public int InsertRoleUser(List<Long> list,Long roleid)throws Exception{
		try{
			int result=0;
			result+=rightsDao.DeleteRoleUser(roleid);
			result+=rightsDao.InsertRoleUserBatch(list,roleid);
			if(result<list.size()){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return -1;
			}
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	
	public List<RoleMenuEntity> GetRoleMenuList(Long roleid)throws Exception{
		try{
			return rightsDao.GetRoleMenuList(roleid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	@Transactional
	public int InsertRoleMenu(List<Long> list,Long roleid)throws Exception{
		try{
			int result=0;
			result+=rightsDao.DeleteRoleMenu(roleid);
			result+=rightsDao.InsertRoleMenuBatch(list,roleid);
			if(result<list.size()){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return -1;
			}
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	
	public List<RoleOrgEntity> GetRoleOrgList(Long roleid)throws Exception{
		try{
			return rightsDao.GetRoleOrgList(roleid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	@Transactional
	public int InsertRoleOrg(List<Long> list,Long roleid)throws Exception{
		try{
			int result=0;
			result+=rightsDao.DeleteRoleOrg(roleid);
			result+=rightsDao.InsertRoleOrgBatch(list,roleid);
			if(result<list.size()){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return -1;
			}
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	public List<RoleEntity> GetRoleListLimit(String where,int beginrow,int endrow)throws Exception{
		try{
			return rightsDao.GetRoleListLimit(where,beginrow,endrow);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	public int GetRoleListLimitCount(String where)throws Exception{
		try{
			return rightsDao.GetRoleListLimitCount(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	public int DeleteRoleUserByUserid(Long roleid,Long userid)throws Exception{
		try{
			return rightsDao.DeleteRoleUserByUserid(roleid,userid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
}
